﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace PJ.Data
{
    /// <summary>
    /// OrderedFragmentPICProvider : Persist In Cache data provider
    /// </summary>
    /// <typeparam name="I">Object to provide interface type</typeparam>
    /// <typeparam name="C">Object to provide concrete class type</typeparam>
    public abstract class OrderedFragmentPICProvider<I, C> : EntityFragmentPICProvider<I, C>, IOrderedFragmentProvider<I>
        where C : BaseOrderedFragmentData, I, new()
        where I : class, IOrderedFragmentData
    {
        public OrderedFragmentPICProvider(ICacheManager cache)
            : base(cache)
        {
        }



        public bool OrderExists(string parentId, double order)
        {
            if ((from d in DataObjects
                 where d.ParentId == parentId && d.Order == order
                 select d).Count() > 0)
                return true;
            else
                return false;
        }
        public I FindByOrder(string parentId, double order)
        {
            var query = (from d in DataObjects
                         where d.ParentId == parentId && d.Order == order
                         select d);
            if (query.Count() > 0)
                return query.Single();
            else
                return default(I);
        }
        public virtual double Up(string parentId, double order)
        {
            if (IsLast(parentId, order))
                throw new ArgumentOutOfRangeException();
            I item = FindByOrder (parentId, order);
            double newOrder = item.Order + ((from d in DataObjects 
                    where d.ParentId == parentId && d.Order > order 
                          select d.Order).Min() - item.Order)/2.0;
            if (OrderExists(parentId, newOrder))
                throw new System.Data.DuplicateNameException();
            return item.Order;
        }

        public virtual double Down(string parentId, double order)
        {
            if (IsFirst(parentId, order))
                throw new ArgumentOutOfRangeException();
            I item = FindByOrder(parentId, order);
            double newOrder = item.Order  - (item.Order - (from d in DataObjects
                                where d.ParentId == parentId && d.Order < order
                                select d.Order).Max()) / 2.0;
            if (OrderExists(parentId, newOrder))
                throw new System.Data.DuplicateNameException();
            return item.Order;
        }

        public virtual bool IsFirst(string parentId, double order)
        {
            if (OrderExists(parentId, order))
                return (from d in DataObjects where d.ParentId == parentId && d.Order < order select d.Order).Count() == 0;
            else
                throw new ArgumentException();
        }

        public virtual bool IsLast(string parentId, double order)
        {
            if (OrderExists(parentId, order))
                return (from d in DataObjects where d.ParentId == parentId && d.Order > order select d.Order).Count() == 0;
            else
                throw new ArgumentException();
        }

        #region Interface override
        IOrderedFragmentData IOrderedFragmentProvider.FindByOrder(string parentId, double order)
        {
            return FindByOrder(parentId, order);
        }
        #endregion
    }
}
